/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import facade.facade;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author MADD
 */
@ManagedBean(name = "LoginBean")
@RequestScoped
public class LoginBean {
    private String userName;
    private String userPassword;
    private String Mensaje = "";
    
    public LoginBean() {

    }
    
    public String validate() {
        // Validar si la contraseña y pass son correctas.
        // si hay un error return null;
        facade fachada = new facade();
        Boolean usuario = false;
        String result = "";
        usuario = fachada.validarUsuario(this.userName, this.userPassword);
        
        if(usuario == true){
            UserBean user = new UserBean();
            user.setUserName(userName);
            user.setUserPassword(userPassword);
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
            facesContext.getExternalContext().getSessionMap().put("UserBean", user);
            result = "admin/index";
            Mensaje = "";
        } else {
            Mensaje = "Nombre de usuario o contraseña incorrectos";
            result = null;
        }
        
        return result;

    }
    
    public String cerrar() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

        session.removeAttribute("UserBean");
        session.invalidate(); 
        
        return "/login";
    }

    public String getUserName() {
	return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getMensaje() {
        return Mensaje;
    }

    public void setMensaje(String Mensaje) {
        this.Mensaje = Mensaje;
    }

}
